<template>
	<div class="authorize">
		<bread-crumb>授权中心</bread-crumb>
		<div class="uc-body">
			<div class="control-bar">
				<span class="list-title">授权列表</span>
				<el-button type="primary" size="small" @click="reTestApproveSatus()">手动检测</el-button>
			</div>
			<table class="table-authorize">
				<thead>
					<tr>
						<th>类型</th>
						<th>标题</th>
						<th>状态</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<tr v-for="item in contractData">
						<td>{{tagCnMap[item.tag]}}</td>
						<td class="c-blue">{{item.title}}</td>
						<td>
							<span class="c-green" v-if="item.isAuthorized==true">已授权</span>
							<span class="c-red" v-else-if="item.isAuthorized==false">未授权</span>
							<span v-else class="c-999">检测中...</span>
						</td>
						<td>
							<div v-if="item.isAuthorized==false">
								<el-button size="small" type="primary" @click="authorize(item)">授权</el-button>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
	</div>
</template>

<script>

//数据
import breadCrumb from '../components/breadCrumb.vue'
import { approve, getBalanceAndApprove2, getTransactionInfo, hex2Num, INITDATA_TIME_LIMIT } from "@/libs/tron.lib";
import { mixins } from '@/mixins/mixins'
import { mixinsUtils } from '@/mixins/mixins.utils'
import { farmData,gameData } from "@/libs/authorizeList";
import { msgInterface } from "@/libs/site.utils";

//导出
export default {
	name:"authorize",
	components: { breadCrumb },
	mixins:[mixins,mixinsUtils],
	data () {
		return {
			//合约数据
			contractData:[...farmData,...gameData],
			//类型映射中文
			tagCnMap:{
				farm:'矿池',
				game:'游戏',
			}
		}
	},
	created () {

	},
	mounted () {
		window.setTimeout(() => {
			this.loadBalanceAndApproveGroup()
		},INITDATA_TIME_LIMIT)

	},

	methods:{

		/*
		* @desc:手动重新刷新状态（暂时抽离为一个方法）
		* @date: 2021-01-09
		*/
		reTestApproveSatus () {
			this.loadBalanceAndApproveGroup()
		},

		/*
		* @desc: 读取授权信息集中处理，目前直接循环请求
		* @date: 2021-01-08
		*/
		loadBalanceAndApproveGroup () {
			this.contractData.forEach((item) => {
				if (item.isAuthorized == null) {
					if (item.tag == 'farm') {
						console.log('查询矿池授权，地址：',item.contract)
						this.getApproveForMinePool(item)
					} else if (item.tag == 'game') {
						console.log('查询游戏授权，地址：',item.contract)
					}
				} else {
					console.warn('地址：',item.contract,'已经查询过，不再重复查询')
				}
			})
		},




		/*
		* @desc: 获取矿池的授权数据
		* @date: 2021-01-09
		*/
		getApproveForMinePool (item) {
			let that = this
			getBalanceAndApprove2(item.BaseToken, item.contract, that.sender, (rsp) => {
				if (rsp.length === 256) {
					// let approve = rsp.substring(192);
					let _approve = tronWeb.toBigNumber("0x" + rsp.substring(192)) > 0
					item.isAuthorized = _approve //是否已經授權
					console.log('合约地址：',item.contract,'检测授权结果：',_approve)
				}
			});
		},



		/*
		* @desc: 授权操作 ：已测试
		* @date: 2021-01-09
		*/
		authorize (item) {
			let that = this;
			msgInterface(that.LangSet(['授權', 'Approve']), '', 1, '', item.coin);
			//授权方法，里面
			approve(item.contract, item.BaseToken, that.sender, (status, txid) => {
				if (status === 0) { //失敗
					msgInterface(that.LangSet(['授權', 'Approve']), '', -1, '', item.coin);
				} else if (status === 1) { //成功
					msgInterface(that.LangSet(['授權', 'Approve']), '', 2, 'https://tronscan.io/#/transaction/' + txid, item.coin);
					item.isAuthorized = true// 授权成功时，马上改状态
				} else if (status === -1) { //取消
					msgInterface(that.LangSet(['授權', 'Approve']), '', 3, '', item.coin);
				}
			}, (txid, status) => {
				if (status === "SUCCESS") {
					that.$message({
						type: "success",
						message: that.LangSet(['授權', 'Approve']) + item.coin + that.LangSet(['成功', 'SUCCESS'])
					});
				} else if (status === "OUT_OF_ENERGY") {
					msgInterface(that.LangSet(['錢包中請至少保留', 'Minimum wallet balance']), 20, 0, '', "TRX");
				} else {
					that.$message({
						type: "error",
						message: that.LangSet(['授權', 'Approve']) + item.coin + that.LangSet(['失敗', 'FAIL'])
					});
				}

			});
		},
	}
}
</script>

<style lang="less" scoped>
	/*公共变量*/
	@import '~@/assets/global.less';
	@lineColor:#ebeef5;
	.control-bar {display: flex;align-items: center;justify-content: space-between;}
	.list-title{font-size: 16px;color: #2D93CA}
	.table-authorize {
		width:100%;border:1px solid @lineColor;
		th,td {padding:10px 5px;border:1px solid @lineColor}
		tr{
			&:hover{background-color: #f5f7fa;}
		}
		th{text-align: left;}
		td{}
	}
	/************************************适应***************************************/
	@media screen and (max-width: @MINSCREEN){

	}
</style>
